import copy
import json
import cv2
from cv2 import mean
import numpy as np
import math
import time
from colorama import Fore, Back, Style
from colorama import init
import matplotlib.pyplot as plt

import threading
import multiprocessing

from PCGWorker import *

def main():

    # create worker
    PCGWorker_ = PCGWorker(9,9)

    corner_num_max = 0
    ramp_num_max = 0
    while True:
        # generate seed and render
        start = time.time()
        seed = PCGWorker_.generate()
        # seed = PCGWorker_.from_file("new.json")
        end = time.time() - start

        # seed.wave_collapsed
        corner_num = 0
        ramp_num = 0
        for i in range(0,9):
            for j in range(0,9):
                tile_ = seed.wave_oriented[j*9+i][0][0]
                rot = seed.wave_oriented[j*9+i][0][1]
                if tile_>=6 and tile_<=9:
                    corner_num += 1
                elif tile_>=10 and tile_<=13:
                    ramp_num += 1

        if corner_num_max < corner_num:
            corner_num_max = corner_num
        if ramp_num_max < ramp_num:
            ramp_num_max = ramp_num

        print("corner_num_max : ", corner_num_max)
        print("ramp_num_max : ", ramp_num_max)

        PCGWorker_.render(seed,wind_name = "canvas",write_ = False,write_id = 0,output = False,verbose = False)
        # print("Time to generate: " + str(end))
        cv2.waitKey(1)

    # write wave to json file
    # PCGWorker_.to_file(seed,filename = "wave.json")

    # _,_ = PCGWorker_.connectivity_analysis(seed)

if __name__ == "__main__":
    main()
